# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=x265
pkgver=3.5
pkgrel=3
pkgdesc="Open Source H265/HEVC video encoder"
url="https://www.videolan.org/developers/x265.html"
arch="all"
license="GPL-2.0-or-later"
makedepends="cmake nasm ninja numactl-dev"
subpackages="$pkgname-dev $pkgname-libs"
source="https://bitbucket.org/multicoreware/x265_git/downloads/x265_$pkgver.tar.gz"
builddir="$srcdir/${pkgname}_$pkgver"

# See comment on CMAKE_BUILD_TYPE.
export CFLAGS=${CFLAGS/-Os/}
export CXXFLAGS=${CXXFLAGS/-Os/}

build() {
	local cmake_opts=""
	case "$CARCH" in
		# It has textrel on x86 so we disable asm.
		x86) cmake_opts="-DENABLE_ASSEMBLY=OFF";;
		ppc*) cmake_opts="-DENABLE_ALTIVEC=OFF -DCPU_POWER8=OFF";;
		# Fails to build 10 and 12 bit versions
		aarch64) cmake_opts="-DENABLE_ASSEMBLY=OFF";;
		# See https://bitbucket.org/multicoreware/x265_git/issues/559
		*) cmake_opts="-DCMAKE_ASM_NASM_FLAGS=-w-macro-params-legacy";;
	esac

	# CMAKE_BUILD_TYPE - Don't change to None! This is a video encoder,
	#   performance is the most important.
	cmake -B build-12 -S source -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DHIGH_BIT_DEPTH=TRUE \
		-DMAIN12=TRUE \
		-DEXPORT_C_API=FALSE \
		-DENABLE_CLI=FALSE \
		-DENABLE_SHARED=FALSE \
		$cmake_opts
	cmake --build build-12

	cmake -B build-10 -S source -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DHIGH_BIT_DEPTH=TRUE \
		-DEXPORT_C_API=FALSE \
		-DENABLE_CLI=FALSE \
		-DENABLE_SHARED=FALSE \
		$cmake_opts
	cmake --build build-10

	cmake -B build -S source -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DEXTRA_LIB='x265_main10.a;x265_main12.a' \
		-DEXTRA_LINK_FLAGS='-L.' \
		-DLINKED_10BIT=TRUE \
		-DLINKED_12BIT=TRUE \
		$cmake_opts
	ln -s ../build-10/libx265.a build/libx265_main10.a
	ln -s ../build-12/libx265.a build/libx265_main12.a
	cmake --build build
}

check() {
	./build/x265 --version
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

sha512sums="
230e683239c3e262096ba96246c6f67229a1625d163f86647a411733bb1cf349685858aee3017bce818bb6992448d0abaa9241615a5b620561ce47ecb164f997  x265_3.5.tar.gz
"
